﻿@using DeliveryPlus.Web.DAL
@using DeliveryPlus.Web.Classes.Enums
@model System.Collections.Generic.List<DeliveryPlus.Web.DAL.Entities.Cafe>

@{
    ViewBag.Title = "Список заведений";
    Layout = "../Shared/Layout.cshtml";
    var districts = new DeliveryPlusDataContext().Districts.ToList();
    var cafes = new DeliveryPlusDataContext().Cafes.ToList();
    string cafeName = string.Empty;
    string cafeUrl = string.Empty;
    int selectedTypeId = -1;
    int selectedDistrictId = -1;
    string selectString = String.Empty;
}

<script type="text/javascript">
    $(document).ready(function () {
        $(".dropdown img.flag").addClass("flagvisibility");

        $(".dropdown dt a").click(function () {
            $(".dropdown dd ul").toggle();
            $(".dropdown dt a").toggleClass("expanded");
        });

        $(".dropdown dd ul li a").click(function () {
            var text = $(this).html();
            $(".dropdown dt a span").html(text);
            $(".dropdown dd ul").hide();
            //$("#result").html("Selected value is: " + getSelectedValue("districts"));
        });

        function getSelectedValue(id) {
            return $("#" + id).find("dt a span.value").html();
        }

        $(document).bind('click', function (e) {
            var $clicked = $(e.target);
            if (!$clicked.parents().hasClass("dropdown")) {
                $(".dropdown dd ul").hide();
                $(".dropdown dt a").toggleClass("expanded");
            }
        });


        $("#flagSwitcher").click(function () {
            $(".dropdown img.flag").toggleClass("flagvisibility");
        });
    });
</script>

<div id="page-cafes" class="page">
    <div class="page-title">
        <img src="~/Content/images/cafe/title.png" />
    </div>

    <div style="overflow: hidden; height: 40px;">
        <div class="dishListMenu dishListMenuHeader">
            Район
        </div>
        <div style="float: left; height: 65px; width: 650px; display: block; color: #e7323b; text-align: center; line-height: 35px; font-family: Tahoma; font-size: 14px;">
            Кухни
        </div>
    </div>
    <div style="background: url(../Content/images/cafe/white-dot.png) left bottom repeat-x; height: 10px; width: 920px; margin: 0 auto;">
        &nbsp;
    </div>
    <div style="padding-top: 10px;">
        <div class="dishListMenu">
            По какому району отображать кухни
            <br />
            <br />

            <dl id="districts" class="dropdown">
                @if (String.IsNullOrEmpty(Request.QueryString["district"]))
                { 
                    <dt><a href="#"><span class="">Все районы</span></a></dt>
                    <dd>
                        <ul>
                            @foreach (var district in districts)
                            {
                                <li><a href="/cafe?district=@district.Id">@district.Title<span class="value">@district.Id</span></a></li>
                            }
                        </ul>
                    </dd>
                }
                else
                {
                    selectedDistrictId = (!String.IsNullOrEmpty(Request.QueryString["district"]) ? Convert.ToInt32(@Request.QueryString["district"]) : -1);
                    var currentDistrict = districts.Where(i => i.Id == selectedDistrictId).FirstOrDefault();
                    <dt><a href="#"><span class="">@currentDistrict.Title</span></a></dt>
                    <dd>
                        <ul>
                            <li><a href="/cafe">Все районы<span class="value">-1</span></a></li>
                            @foreach (var district in districts)
                            {
                                if (district.Id != selectedDistrictId)
                                { 
                                <li><a href="/cafe?district=@district.Id">@district.Title<span class="value">@district.Id</span></a></li>
                                }
                            }
                        </ul>
                    </dd>
                }
            </dl>
        </div>
        <div style="float: left; height: 65px; display: block; color: #e7323b; text-align: center; font-family: Tahoma; width: 650px; margin-left: 5px;">
            <div class="type-selector">
                <ul class="type-filter">
                    @for (int i = 0; i < Enum.GetNames(typeof(CafeTypes)).Length; i++)
                    {
                        int typeId = Convert.ToInt32(Enum.Parse(typeof(CafeTypes), Enum.GetNames(typeof(CafeTypes))[i]));
                        selectedDistrictId = (!String.IsNullOrEmpty(Request.QueryString["district"]) ? Convert.ToInt32(@Request.QueryString["district"]) : -1);
                        if (typeId != 0)
                        {

                            cafeName = Enum.GetNames(typeof(CafeTypes))[i].Replace('_', ' ');
                            cafeUrl = "/cafe?" + (!String.IsNullOrEmpty(Request.QueryString["district"]) ? "district=" + selectedDistrictId + "&" : string.Empty) + "type=" + typeId;

                            var cCount = cafes.Where(j => j.Types.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Contains(typeId.ToString()) && !j.Hidden);
                            if (selectedDistrictId != -1)
                            {
                                cCount = cCount.Where(j => j.DistrictId == selectedDistrictId);
                            }

                        <li><a href="@cafeUrl">@cafeName 
                            @if (cCount.Count() > 0)
                            { 
                            <span class="whiteText">(@cCount.Count())</span>
                            }
                            </a></li>
                            if (i % 3 == 0 && i != Enum.GetNames(typeof(CafeTypes)).Length)
                            { 
                        @Html.Raw("</ul>");
                        @Html.Raw("<ul class=\"type-filter\">");
                            }
                        }
                    }
                </ul>
            </div>
        </div>
    </div>
    <div class="hr">
        <img src="~/Content/images/cafe/hr.png" />
    </div>
    @if (Model.Where(i => !i.Hidden).Count() == 0)
    {
        <div class="no-result">К сожалению, у нас не найдено заведений, удовлетворяющих требуемым вам условиям. Пожалуйста, ознакомьтесь со <a href="/cafe">ВСЕМИ ЗАВЕДЕНИЯМИ</a>, с которыми сотрудничает наша компания</div>
    }
    else
    {
        <div class="selectString">
            @{
                selectedTypeId = (!String.IsNullOrEmpty(Request.QueryString["type"]) ? Convert.ToInt32(@Request.QueryString["type"]) : -1);
                string typeName = Enum.GetName(typeof(CafeTypes), selectedTypeId);

                if (selectedDistrictId != -1 && !String.IsNullOrEmpty(typeName))
                {
                    selectString = @String.Format("<span class=\"whiteText\">Ваш выбор :</span> {0} в районе {1}", typeName.Replace("_", " "), districts.Where(district => district.Id == selectedDistrictId).FirstOrDefault().Title);
                }
                else if (selectedDistrictId != -1 && String.IsNullOrEmpty(typeName))
                {
                    selectString = @String.Format("<span class=\"whiteText\">Ваш выбор :</span> все кухни в районе {0}", districts.Where(district => district.Id == selectedDistrictId).FirstOrDefault().Title);
                }
                else if (selectedDistrictId == -1 && !String.IsNullOrEmpty(typeName))
                {
                    selectString = @String.Format("<span class=\"whiteText\">Ваш выбор :</span> {0} во всех районах", typeName.Replace("_", " "));
                }
                else
                {
                    selectString = "<span class=\"whiteText\">Ваш выбор :</span> все кухни во всех районах"; 
                }
            }
            
            @Html.Raw(selectString)
                        
        </div>
        <div id="cafe-list">
            @foreach (var cafe in Model.Where(c => !c.Hidden))
            {
                <div class="cafe">
                    <div class="cafe-image" onclick="window.location.href='/cafe/@cafe.Id/dishes'">
                        @if (!String.IsNullOrEmpty(cafe.Logo))
                        {
                            <img class="lazy" data-original="/Files/Logo/@cafe.Logo" alt="@cafe.Title, Хабаровск"/>
                        }
                    </div>
                    <div class="cafe-title">
                        <a href="/cafe/@cafe.Id/dishes">@cafe.Title</a>
                    </div>
                </div>
            }
        </div>
    }
    <div class="hr">
        <img src="~/Content/images/cafe/hr.png" />
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var items = $(".type-filter li");
        for (var i = items.length - 1; i > 0; i--) {
            var item = items[i];
            if (window.location.href.indexOf($(item).find("a").attr("href")) != -1) {
                $(item).addClass("active");
                break;
            }
        }
    });
</script>
